home *** CD-ROM | disk | FTP | other *** search
- /* UNIQ.C
- *
- * Source Code by Oliver Kaufmann
- *
- * $VER: uniq 1.02 (25.3.1994) © by Oliver Kaufmann
- *
- * Compile with Manx Aztec or any other C-Compiler
- *
- */
- #include <stdlib.h>
- #include <stdio.h>
- #include <dos/dos.h>
- #include <functions.h>
-
- void CloseAll(char *s);
- char *makename(char *s, int blen, int elen, char **tb, char **tex, char *buffer);
- char *modifyname(char *tb, int blen, int elen, long sernum, int origlen);
- char *savestring(char *s);
- struct tbl *feedtbl(char *s, struct tbl *firsttbl);
- struct tbl *lookup(char *t, struct tbl *firsttbl, struct tbl *excludetbl);
- struct tbl *modifytbl(char *s, char *t, struct tbl *thistbl);
- struct tbl *inserttbl(char *s, char *t, struct tbl *firsttbl);
- struct tbl *uniq(struct tbl *thistbl, struct tbl *firsttbl,int blen, int elen);
- struct tbl *makeuniq(struct tbl *firsttbl, int blen, int elen);
- void giveout(struct tbl *firsttbl, char *lformat, int allflag);
-
-
- char ver[] = "$VER: uniq 1.02 (25.3.1994) © by Oliver Kaufmann";
- char *usage = "uniq [-d[<dir>]] [-a] [-f] [-l<lformat>] -p<pattern> -b<basenamelength> -e<extensionlength>\n\
- -d<dir> : use contents of <dir> as input, otherwise use stdin\n\
- -a : print all names, even those which do not change\n\
- -f : process files only, no dirs (requires -d option)\n\
- -l<lformat> : use <lformat> for formated output (\"rename %s %s\")\n\
- -p<pattern> : pattern to apply (default #?)\n\
- -b<basenamelength> : max. length of the uniqified string (default 8)\n\
- -e<extensionlength> : max. length of the uniqified extension (default 3)\n";
-
-
- struct tbl {
- char *orig;
- char *new;
- struct tbl *nexttbl;
- };
-
- struct MyAnchorPath {
- struct AChain *ap_Base; /* pointer to first anchor */
- struct AChain *ap_Last; /* pointer to last anchor */
- LONG ap_BreakBits; /* Bits we want to break on */
- LONG ap_FoundBreak; /* Bits we broke on. Also returns ERROR_BREAK */
- BYTE ap_Flags; /* New use for extra word. */
- BYTE ap_Reserved;
- WORD ap_Strlen; /* This is what ap_Length used to be */
- struct FileInfoBlock ap_Info;
- UBYTE ap_Buf[256]; /* Buffer for path name, allocated by user */
- };
-
- struct FileInfoBlock *fib;
- struct MyAnchorPath myap;
- struct AnchorPath *ap;
-
-
- struct FileInfoBlock finfo;
-
- #define BUFLEN 256
-
- char pbuf[BUFLEN];
-
- void main(int argc, char *argv[])
- {
- struct tbl *firsttbl=NULL;
- BPTR lock, oldlock;
- char lbuf[BUFLEN];
-
- int allfilendirs = 1;
- int allflag = 0;
- int blen=8;
- int elen=3;
- char *dirname=NULL;
- char *lformat = "rename %s %s";
- char *pattern="#?";
-
- char *s, *opt;
- int i, res;
-
- for(i=1; i<argc; i++) {
- if( *(opt=argv[i]) == '-') {
- switch(opt[1]) {
- case 'd' : dirname = &opt[2]; break;
- case 'a' : allflag = 1 ; break;
- case 'f' : allfilendirs = 0 ; break;
- case 'l' : lformat = &opt[2]; break;
- case 'p' : pattern = &opt[2]; break;
- case 'b' : if( (blen = atoi(&opt[2]) ) <= 0)
- CloseAll("basenamelength must be > 0");
- break;
- case 'e' : if( (elen = atoi(&opt[2])) < 0)
- CloseAll("extensionlength must be >= 0");
- break;
- default : printf("unknown option: ignored\n");
- break;
- }
- } else if( *opt == '?')
- CloseAll(usage);
- }
-
- if(dirname != NULL) {
- if( (lock=Lock(dirname,ACCESS_READ)) == 0)
- CloseAll("no lock on dir");
- oldlock = CurrentDir(lock);
-
- ap = (struct AnchorPath*)&myap;
- ap->ap_Strlen = 254;
- ap->ap_BreakBits = 0;
-
- if ( MatchFirst(pattern, ap) )
- CloseAll("MatchFirst: no match");
-
- do {
- fib = &ap->ap_Info;
- if( allfilendirs || (fib->fib_DirEntryType < 0) ) /* exclude dirs on demand */
- firsttbl = feedtbl(fib->fib_FileName, firsttbl);
- } while (!MatchNext(ap));
-
- MatchEnd(ap);
- CurrentDir(oldlock);
- UnLock(lock);
- } else {
- if( ParsePattern(pattern, pbuf, BUFLEN) == -1)
- CloseAll("pattern too long");
- while( (res=scanf("%s",lbuf)) != EOF) {
- if(res==0)
- CloseAll("res = 0");
- if(MatchPattern(pbuf, lbuf)) /* nur wenn pattern matched */
- firsttbl = feedtbl(lbuf, firsttbl);
- }
- }
-
- if( (firsttbl=makeuniq(firsttbl, blen, elen)) == NULL)
- CloseAll("unable to uniqify");
-
-
- giveout(firsttbl, lformat, allflag);
-
- CloseAll(NULL);
- }
-
- struct tbl *makeuniq(struct tbl *firsttbl, int blen, int elen)
- {
- struct tbl *thistbl;
-
- thistbl = firsttbl;
-
- while(thistbl != NULL) {
- if( uniq(thistbl, firsttbl, blen, elen) == NULL)
- return NULL;
-
- thistbl = thistbl->nexttbl;
- }
- return firsttbl;
- }
-
- struct tbl *uniq(struct tbl *thistbl, struct tbl *firsttbl,int blen, int elen)
- {
- char lbuf[BUFLEN];
- char buffer[BUFLEN];
-
- char *t, *tb, *tex;
- char *s;
-
- long sernum;
- int origlen;
-
- s = thistbl->orig;
- makename(s, blen, elen, &tb, &tex, buffer); /* name und extension */
- origlen = strlen(tb); /* base name length */
-
- for(sernum = 0; ; sernum++) {
- strcpy(lbuf,tb);
- strcat(lbuf,tex);
- if( lookup(lbuf, firsttbl, thistbl) == NULL )
- break;
- if( modifyname(tb, blen, elen, sernum, origlen) == 0)
- return NULL;
- }
-
- t = savestring(lbuf);
-
- modifytbl(s, t, thistbl);
-
- return thistbl;
- }
-
- struct tbl *inserttbl(char *s, char *t, struct tbl *firsttbl)
- {
- struct tbl *thistbl;
-
- if( (thistbl=malloc(sizeof(struct tbl))) == NULL)
- CloseAll("no memory for tbl");
-
- thistbl->orig = s;
- thistbl->new = t;
- thistbl->nexttbl = firsttbl;
- return thistbl;
-
- }
-
- struct tbl *modifytbl(char *s, char *t, struct tbl *thistbl)
- {
- thistbl->orig = s;
- thistbl->new = t;
- return thistbl;
- }
-
- struct tbl *feedtbl(char *s, struct tbl *firsttbl)
- {
- char *m;
-
- m = savestring(s);
- return inserttbl(m, m, firsttbl);
- }
-
- char *savestring(char *s)
- {
- char *mem;
-
- if( (mem=malloc(strlen(s)+1)) == NULL)
- CloseAll("no memory for string");
-
- strcpy(mem, s);
- return mem;
- }
-
- struct tbl *lookup(char *t, struct tbl *firsttbl, struct tbl *excludetbl)
- {
- while(firsttbl != NULL) {
- if( (strcmp(t, firsttbl->new) == 0) && (firsttbl != excludetbl) )
- break;
- firsttbl = firsttbl->nexttbl;
- }
- return firsttbl;
- }
-
-
- char *modifyname(char *tb, int blen, int elen, long sernum, int origlen)
- {
- char lbuf[BUFLEN];
- int sl, i, j, pos;
-
- sprintf(lbuf,"%x",sernum);
- if( (sl = strlen(lbuf)) > blen ) /* sernum zu groß */
- return NULL;
-
- pos = origlen;
- if( origlen + sl > blen )
- pos = blen-sl;
- strncpy(tb+pos, lbuf, sl);
- return tb;
- }
-
- char *makename(char *s, int blen, int elen, char **tb, char **tex, char *buffer)
- {
- char *t;
- int n,m;
- int sl,i;
-
- *tb = buffer;
-
- for(n=0, t=s ; (*t != '\0') && (*t != '.') ; *t++, n++);
-
- strncpy(buffer, s, n); /* es wird evtl mehr kopiert */
-
- while(n<blen)
- buffer[n++] = '\0'; /* auffüllen */
-
- buffer[blen] = '\0'; /* rest abschneiden */
-
- n = blen+1; /* ab hier extension */
-
- buffer[n] = '\0'; /* default ist leer */
-
- *tex = &buffer[n];
-
- if(elen != 0 ) {
-
- for(i = sl = strlen(s)-1; sl >= 0; sl--)
- if(s[sl] == '.')
- break;
- if(s[sl] == '.') {
- buffer[n++] = '.';
- strncpy(&buffer[n], &s[sl+1], i-sl+1); /* 0 mitkopieren */
- n += i-sl;
- }
-
- buffer[n] = '\0'; /* default ist leer */
- buffer[blen+elen+1+1]='\0';
-
- }
-
- return *tb;
-
- }
-
- void giveout(struct tbl *firsttbl, char *lformat, int allflag)
- {
- while (firsttbl != NULL) {
- if( allflag || strcmp(firsttbl->orig, firsttbl->new) ) {
- printf(lformat,firsttbl->orig, firsttbl->new);
- printf("\n");
- }
- firsttbl = firsttbl->nexttbl;
- }
- }
-
-
- void CloseAll(char *s)
- {
- if(s != NULL)
- printf("%s\n",s);
- exit(0);
- }
-